<?php

namespace SLN0002\Controller;

use Home\Common\FIdConst;
use Home\Controller\PSIBaseController;
use Home\Service\UserService;
use SLN0002\Service\CAService;

/**
 * 结账Controller
 *
 * @author PSI
 * @copyright 2015 - present
 * @license GPL v3
 */
class CAController extends PSIBaseController
{
  /**
   * 结账 - 主页面
   * 
   * 模板页面：web\Application\SLN0002\View\CA\index.html
   */
  public function index()
  {
    $us = new UserService();

    if ($us->hasPermission(FIdConst::GL_CLOSE_ACC)) {
      $this->initVar();

      $this->assign("title", "结账");

      $this->display();
    } else {
      $this->gotoLoginPage("/SLN0002/CA/index");
    }
  }

  /**
   * 返回所有的公司列表
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\CA\CloseAccMainForm.js
   */
  public function companyList()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::GL_CLOSE_ACC)) {
        die("没有权限");
      }

      $service = new CAService();
      $this->ajaxReturn($service->companyList());
    }
  }

  /**
   * 执行结账操作
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\CA\CloseAccMainForm.js
   */
  public function actionCloseAcc()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::GL_CLOSE_ACC)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
      ];
      $service = new CAService();
      $this->ajaxReturn($service->actionCloseAcc($params));
    }
  }

  /**
   * 结转损益
   * 
   * JS: web\Public\Scripts\PSI\SLN0002\CA\CloseAccMainForm.js
   */
  public function actionLossAndProfit()
  {
    if (IS_POST) {
      $us = new UserService();

      if (!$us->hasPermission(FIdConst::GL_CLOSE_ACC)) {
        die("没有权限");
      }

      $params = [
        "companyId" => I("post.companyId"),
      ];
      $service = new CAService();
      $this->ajaxReturn($service->actionLossAndProfit($params));
    }
  }
}
